package com.cn.jx.mongo.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.cn.jx.base.AbstractBase;
import com.cn.jx.enumType.MongoSqlRelationEnum;
import com.cn.jx.mongo.IMongoService;
import com.cn.jx.queryTool.ConditionField;
import com.jx.util.MapObject;

@Service("mongoService") 
public class MongoServiceImpl<T> extends AbstractBase implements IMongoService<T> {

	@Autowired
    private MongoTemplate mongoTemplate;
	
	@Override
	public void saveItem(T item) {
		mongoTemplate.save(item);
	}

	@Override
	public List<MapObject> findList(List<ConditionField> filed,MongoSqlRelationEnum ... relation) {
		MongoSqlRelationEnum sqlReal=MongoSqlRelationEnum.AND;
		if(relation!=null) {
			for(MongoSqlRelationEnum sq:relation) {
				sqlReal=sq;
			}
		}
		Criteria[] criteria =getCriteria(filed);
		Query query=new Query();
		if(MongoSqlRelationEnum.AND.equals(sqlReal)) {
			query.addCriteria(new Criteria().andOperator(criteria));
		}
		if(MongoSqlRelationEnum.OR.equals(sqlReal)) {
			query.addCriteria(new Criteria().orOperator(criteria));
		}
		List<MapObject> list=mongoTemplate.find(query,  MapObject.class);
		return list;
	}

}
